/**
 * 
 */
package kmi.droid.animatron;

/**
 * @author kami defines 2d vector with float coordinates X and Y
 */
public class Vector2
{
	public float X;
	
	public float Y;

	public Vector2()
	{
		this(0, 0);
	}

	public Vector2(float x, float y)
	{
		X = x;
		Y = y;
	}

	public static Vector2 zero()
	{
		return new Vector2(0, 0);
	}

	public static Vector2 one()
	{
		return new Vector2(1, 1);
	}

	public static Vector2 add(Vector2 a, Vector2 b)
	{
		return new Vector2(a.X + b.X, a.Y + b.Y);
	}

	public static Vector2 subtract(Vector2 a, Vector2 b)
	{
		return Vector2.add(a, Vector2.multiply(b, -1));
	}

	public static Vector2 multiply(Vector2 v, float scalar)
	{
		return new Vector2(v.X * scalar, v.Y * scalar);
	}

	public static Vector2 multiply(float scalar, Vector2 v)
	{
		return new Vector2(v.X * scalar, v.Y * scalar);
	}

	public static Vector2 divide(Vector2 v, float scalar)
			throws IllegalArgumentException
	{
		if (scalar == 0)
			throw new IllegalArgumentException("Divider cannot be zero.");

		return Vector2.multiply(1 / scalar, v);
	}

	public static float length(Vector2 v)
	{
		return (float) Math.sqrt(v.X * v.X + v.Y * v.Y);
	}

	public static float distance(Vector2 p1, Vector2 p2)
	{
		return Vector2.length(Vector2.subtract(p1, p2));
	}

	public Vector2 getNormal()
	{
		return Vector2.divide(this, Vector2.length(this));
	}
}
